//
//  DataFrameWebTool.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/26.
//

#import "DataFrameWebTool.h"
#import "DataFrameParam.h"


@implementation DataFrameWebTool
- (void)WebConfigMake:(UIView *)supView path:(NSString *)path {
    self.webTool = [[WKWebView alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT + 44, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_BAR_HEIGHT - 44) configuration:[self configMake]];
    if (@available(iOS 11.0, *)) {
        self.webTool.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.webTool.backgroundColor = HexColor(@"#F0FDC1");
    self.webTool.UIDelegate = self;
    self.webTool.scrollView.delegate = self;
    self.webTool.navigationDelegate = self;
    self.webTool.allowsBackForwardNavigationGestures = YES;
    [supView addSubview:self.webTool];
    
    NSString *url = @"";
    NSString *param =[DataFrameParam frameMoverInfoPart];
    param = [param stringByReplacingOccurrencesOfString:@"?" withString:@""];
    if([path containsString:@"?"]){
        url = [NSString stringWithFormat:@"%@&%@",path,param];
    } else {
        url = [NSString stringWithFormat:@"%@?%@",path,param];
    }
    NSLog(@"jamPathWeb %@",url);
    [MBProgressHUD showIconMessage:@"" ToView:nil RemainTime:2];
//    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
//    [SVProgressHUD setMaximumDismissTimeInterval:2];
//    [SVProgressHUD show];
   [self.webTool loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
}


- (WKWebViewConfiguration *)configMake {
    WKWebViewConfiguration *config = [WKWebViewConfiguration new];
    WKUserContentController *content = [[WKUserContentController alloc] init];
    [content addScriptMessageHandler:self name:@"begana"];
    [content addScriptMessageHandler:self name:@"itb"];
    [content addScriptMessageHandler:self name:@"clothesd"];
    [content addScriptMessageHandler:self name:@"fore"];
    [content addScriptMessageHandler:self name:@"anf"];
    [content addScriptMessageHandler:self name:@"trickg"];
    [content addScriptMessageHandler:self name:@"accidentallyh"];
    [content addScriptMessageHandler:self name:@"andi"];
    [content addScriptMessageHandler:self name:@"songsFactj"];
    [content addScriptMessageHandler:self name:@"whok"];
    config.userContentController = content;
    return config;
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    [self.delegate changeTitleName:webView.title];
//    [SVProgressHUD dismiss];
    
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return nil;
}

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    
    if([message.name isEqualToString:@"begana"]){
        [self.delegate begana:message.body];
        return;
    }

    if([message.name isEqualToString:@"itb"]){
        [self.delegate itb];
        return;
    }
    if([message.name isEqualToString:@"clothesd"]){
        [self.delegate clothesd];
        return;
    }
    if([message.name isEqualToString:@"fore"]){
        [self.delegate fore];
        return;
    }
    
    if([message.name isEqualToString:@"anf"]){
        [self.delegate anf];
        return;
        
    }
    if([message.name isEqualToString:@"trickg"]){
        [self.delegate trickg:((NSArray *)message.body).firstObject];
        return;
    }
    
    if([message.name isEqualToString:@"accidentallyh"]){
        [self.delegate accidentallyh];
        return;
        
    }
    if([message.name isEqualToString:@"andi"]){
        [self.delegate andi];
        return;
        
    }
    
    if([message.name isEqualToString:@"songsFactj"]){
        [self.delegate songsFactj];
        return;
    }
    
    if([message.name isEqualToString:@"whok"]){
        [self.delegate whok];
        return;
    }
    
}
@end
